package com.qiangqiang.task.server.mapper;

import com.qiangqiang.task.server.dto.NickNameDTO;
import com.qiangqiang.task.server.entity.UserInfo;
import com.qiangqiang.task.server.mapper.provider.UserMapperProvider;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @program: task
 * @description:
 * @author: Sky.lin
 * @create: 2019-01-30 20:51
 **/
@Mapper
public interface UserMapper {
    //    获取user表中所有记录
    @SelectProvider(type = UserMapperProvider.class, method = "queryAllUser")
    List<UserInfo> queryUsers(@Param("page") int page, @Param("pageSize") int pageSize);

    @Select("select * from user_info")
    List<UserInfo> queryAllUser();

    //    根据用户id查询记录
    @Select("select * from user_info where user_id=#{userId}")
    UserInfo queryUserById(@Param("userId") long userId);

    //    根据用户昵称查询记录
    @Select("select * from user_info where nick_name=#{nickName}")
    UserInfo queryUserByNickName(@Param("nickName") String nickName);

    @Update("update user_info set achievement=achievement+#{value} where user_id=#{userId}")
    int updateAchievementByUserId(@Param("value") int value, @Param("userId") long userId);

    @Update("update user_info set commission=commission+#{value} where user_id=#{userId}")
    int updateCommissionByUserId(@Param("value") BigDecimal value, @Param("userId") long userId);

    /**
     * @param [user]
     * @return int
     * @author sky
     * @date 2018/7/14
     * @describe: 插入更新操作写在一起，当主键不存在时插入，当主键存在时更新
     */
    @InsertProvider(type = UserMapperProvider.class, method = "upSert")
    int upSertUser(@Param("userInfo") UserInfo userInfo);

    //    删除一个用户
    @Delete("delete from user_info where user_id=#{userId}")
    int deleteUser(@Param("userId") long userId);

    //    根据id批量查询用户昵称
    @SelectProvider(type = UserMapperProvider.class, method = "queryNamesByIds")
    List<String> getNicknamesByIds(@Param("userIds") List<Long> userIds);


    @SelectProvider(type = UserMapperProvider.class, method = "queryNickNameDTOByIds")
    List<NickNameDTO> getNickNameDTOByIds(@Param("userIds") List<Long> userIds);

    @Select("select phone from user_info where user_id=#{userId}")
    String queryPhoneByUserId(@Param("userId") long userId);

    @Select("select nick_name from user_info where user_id=#{userId}")
    String queryNickNameByUserId(@Param("userId") long userId);
}
